package cn.lcfms.app.admin.controller;



import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.lcfms.app.admin.service.CommonService;
import cn.lcfms.bin.annotation.PermitPoll;
import cn.lcfms.bin.core.Application;
import cn.lcfms.bin.service.BaseService;


@PermitPoll(type="系统功能",name="插件管理")
@Controller("admin.PlugController")
@RequestMapping("/admin/plug")
public class PlugController extends AdminBaseController {
	
	@Autowired
	private CommonService cs;
		
	@RequestMapping("/message")
	public ModelAndView message() {		
		BaseService service= Application.getService("message");
		service.where("type", "temp");
		List<HashMap<String, Object>> list = service.selectList();
		ModelAndView view=new ModelAndView("admin/frame/message","list",list);
		view.addObject("username", cs.getMessageBase()[0]);
		view.addObject("password", cs.getMessageBase()[1]);
		return view;
	}
	
	@RequestMapping("/saveMessage")
	public String saveMessage(String username,String password) {			
		BaseService service= Application.getService("message");
		service.where("type='username'").setData(username).update("value");
		service.where("type='password'").setData(password).update("value");
		cs.clearMessageBase();
		return "{code:1,msg:修改成功！}";
	}
	
	@RequestMapping("/addTemp")
	public String addTemp(String title,String value) {			
		BaseService service= Application.getService("message");
		service.setData(title,"temp",value).insert("title","type","value");
		return "{code:1,msg:添加成功！,url:message}";
	}
	
	@RequestMapping("/deleteTemp")
	public String deleteTemp(int mid) {			
		BaseService service= Application.getService("message");
		service.deleteById(mid);
		return "{code:1,msg:删除成功！,url:message}";
	}
	
	@RequestMapping("/sendmsg")
	public String sendmsg(String message,String user) {			
		String[] mobiles=user.split(",");
		StringBuilder builder=new StringBuilder();
		for(int i=0;i<mobiles.length;i++) {
			String mobile = mobiles[i];
			try {
				cs.sendMsg(message, mobile);
				builder.append(mobile+"发送成功！<br/>");
			} catch (Exception e) {
				builder.append(mobile+"发送失败！失败原因："+e.getMessage()+"<br/>");
			}
		}
		return "{\"code\":1,\"msg\":\"发送完成<br/>"+builder.toString()+"\",\"url\":\"message\"}";
	}
	
	
}
